﻿using Log;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Threading;
using System.IO;

namespace SmileSolver
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {

        [Import(typeof(Solver))]
        Solver _solver;

        ILogger _log;

        public MainWindow()
        {
            InitializeComponent();

            _log = LogFileFactory.CreateLogFile(Assembly.GetExecutingAssembly().Location + ".log", true, true, true);
            //_solver = new Solver(lf, this.Dispatcher);

            FileInfo fi = new FileInfo(Assembly.GetExecutingAssembly().Location);
            Composition(fi.Directory.FullName);
            DataContext = _solver;
        }

        private void Composition(string dir)
        {            
            var catalog = new AggregateCatalog();
            //catalog.Catalogs.Add(new AssemblyCatalog(Assembly.GetExecutingAssembly()));
            catalog.Catalogs.Add(new DirectoryCatalog(dir));
            CompositionContainer container = new CompositionContainer(catalog);

            try
            {
                container.ComposeExportedValue<ILogger>(_log);
                container.ComposeExportedValue<Dispatcher>(this.Dispatcher);
                container.ComposeParts(this);
            }
            catch (CompositionException compositionException)
            {
                _log.Error(compositionException.Message);
            }
        }

        private void WindowClosed(object sender, EventArgs e)
        {
            _solver.Stop();
        }
    }
}
